package unidt.xf.core.mybatis;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author zjk
 * @Description:
 * @Date 2022/5/11 11:23
 **/
public class PageFactory {

    public static IPage getPage(PageParam pageParam) {

        return new Page(pageParam.getPageNum(), pageParam.getPageSize());
    }

    public static IPage getPage(Integer pageNum, Integer pageSize) {
                if(pageNum==null){
                    pageNum = 0;
                }
                if(pageSize==null){
                    pageSize = 10;
                }
        return new Page(pageNum, pageSize);
    }

    public static IPage transToPage(IPage page, Class cls) {
        IPage iPage = new Page();
        BeanUtils.copyProperties(page, iPage);

        List records = new ArrayList();
        page.getRecords().forEach(i -> {
            try {

                Object obj = cls.newInstance();
                BeanUtils.copyProperties(i,obj);
                records.add(obj);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        page.setRecords(records);
        return  page;
    }


}
